<template>
  <el-dialog
    v-model="dialogVisible"
    title="新增分类"
    width="60%"
    center
    @close="closeDialog(false)"
  >
    <el-form
      :rules="rules"
      :model="form"
      label-width="100px"
      ref="validateForm"
    >
      <el-form-item label="分类名" prop="category_name">
        <el-input v-model="form.category_name" />
      </el-form-item>
      <el-form-item label="分类描述" prop="category_description">
        <el-input v-model="form.category_description" />
      </el-form-item>
      <el-form-item label="分类图片" prop="category_pic">
        <el-upload
          class="avatar-uploader"
          :action="uploadURL"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <!-- 这里不要使用默认的,默认的close不是响应式的 -->
        <el-button
          @click="
            () => {
              closeDialog(false)
            }
          "
        >
          取消
        </el-button>
        <el-button type="primary" @click="submit(data)">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { reactive, toRefs, ref } from 'vue'
import axios from 'axios'

//
export default {
  props: {
    dialogVisible: Boolean,
  },
  emits: ['onCloseDialog'],
  setup(props, { emit }) {
    const validateForm = ref()
    const pic = ref()
    const state = reactive({
      dialogVisible: props.dialogVisible,
      uploadURL: import.meta.env.VITE_APP_URL + '/upload',
      imageUrl: '',
      form: {
        category_name: '',
        category_description: '',
        category_pic: '',
      },
    })
    // 上传成功以后的处理函数
    const handleAvatarSuccess = (response) => {
      state.imageUrl = import.meta.env.VITE_APP_URL + '/' + response.msg
      pic.value = response.msg
      state.form.category_pic = response.msg
    }
    const closeDialog = (visible) => {
      // 使用父组件的方法,方法名,传递的参数
      emit('onCloseDialog', visible)
      // 关闭窗口后清空图片
      state.imageUrl = ''
    }
    // 校验规则
    const rules = {
      category_name: [
        { required: true, message: '用户名不能为空', trigger: 'blur' },
      ],
      category_description: [
        { required: true, message: '用户密码不能为空', trigger: 'blur' },
      ],
      category_pic: [
        { required: true, message: '分类图片不能为空', trigger: 'blur' },
      ],
    }
    // 统一管理添加的方法
    const submit = () => {
      //验证表单的值都填完没有
      validateForm.value.validate((valid) => {
        //console.log(valid) //如果有没填的就是false,都填了就是true
        //获取表单数据
        console.log(pic.value) ///images/测试图片_539.jpg
        if (valid) {
          const obj = {
            category_name: state.form.category_name,
            category_description: state.form.category_description,
            category_pic: pic.value,
          }
          axios.post('/category', obj).then((res) => {
            console.log(res)
            // 提交后重置/清空表单
            validateForm.value.resetFields()
            state.imageUrl = ''
            // 关闭表单
            emit('onCloseDialog', false, res.data)
          })
        }
      })
    }
    return {
      ...toRefs(state),
      closeDialog,
      rules,
      handleAvatarSuccess,
      submit,
      validateForm,
    }
  },
}
</script>

<style>
/* 图片上传样式 */
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}
/* 设置图片上传宽高 */
.avatar {
  width: 100px;
  height: 100px;
}
.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  text-align: center;
}
</style>
